const loginIdValidator = new FieldValidator('txtLoginId',async(val)=>{
    if (!val) {return '请填写账号';}
    const resp = await API.exists(val);
    if(resp.data){
        return '账号已存在！'
    }
    if(val.length >= 20){
        return '长度应小于20!';
    }
});
const nickNameValidator = new FieldValidator('txtNickname',(val)=>{
    if (!val) {return '请填写昵称';}
    if(val.length >= 20){
        return '长度应小于20!';
    }
});

const loginPwdValidator = new FieldValidator('txtLoginPwd',(val)=>{
    if (!val) {return '请填写密码';}
    if(val.length >= 20){
        return '长度应小于20!';
    }
});


const loginPwdConfirmValidator = new FieldValidator('txtLoginPwdConfirm',(val)=>{
    if (!val) {return '请填写确认密码';}
    if(val!==loginPwdValidator.input.value){
        return '两次不一致';
    }
});

const form = $('.user-form');
form.onsubmit = async (e) => {
    e.preventDefault();
    const result = await FieldValidator.validate(loginIdValidator,nickNameValidator,loginPwdValidator,loginPwdConfirmValidator);
    if (!result){return ;}
    const formData = new FormData(form);
    const data = Object.fromEntries(formData.entries());
    console.log(data);
    const resp = await API.reg(data);
    if(resp.code === 0){alert('注册成功');
    location.href = './login.html';
}
}